﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arduino.ControlPanel
{
    public partial class ucPinAnalog : UserControl, IUcPin 
    {
        private Firmata.Types.Pins mPin;
        private Firmata.Firmata mFirmata;
        private bool mReportLocal = false;

        public ucPinAnalog(Firmata.Types.Pins Pin, Firmata.Firmata _Firmata)
        {
            InitializeComponent();
            mPin = Pin;
            mFirmata = _Firmata;

            groupBox.Text = Pin.ToString();
            try
            {
                comboBoxMode.Enabled = false;
                comboBoxMode.DataSource = Enum.GetValues(typeof(Firmata.Types.PinMode));
                comboBoxMode.SelectedItem = Firmata.Types.PinMode.Analog;
            }
            catch (Exception)
            { }
        }

        public Firmata.Types.Pins GetPin()
        {
            return mPin;
        }

        public void RefreshPin()
        {
            if (mFirmata != null)
            {
                bool mode = mFirmata.getPinReportMode(mPin);
                mReportLocal = true;
                checkBoxReporting.Checked = mFirmata.getPinReportMode(mPin);

                if(mFirmata.pinsValues.ContainsKey(mPin)) labelValue.Text = mFirmata.pinsValues[mPin].ToString();
                else labelValue.Text = "0";
            }
        }

        private bool IsSimulation()
        {
            try
            {
                return checkBoxSimulation.Checked;
            }
            catch (Exception) { }
            return false;
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            if (mFirmata != null)
            {
                if(mFirmata.pinsValues.ContainsKey(mPin)) mFirmata.pinsValues[mPin] = trackBar1.Value;
                else mFirmata.pinsValues.Add(mPin,trackBar1.Value);

                RefreshPin();
            }
        }

        private void checkBoxSimulation_CheckedChanged(object sender, EventArgs e)
        {
            if (mFirmata != null)
            {
                groupBoxSimulation.Visible = IsSimulation();
                if (IsSimulation() && mFirmata.pinsValues.ContainsKey(mPin)) trackBar1.Value = mFirmata.pinsValues[mPin];
            }
        }

        private void groupBoxSimulation_EnabledChanged(object sender, EventArgs e)
        {
            foreach (Control control in groupBoxSimulation.Controls)
                control.Enabled = this.groupBoxSimulation.Enabled;
        }

        private void checkBoxReporting_CheckedChanged(object sender, EventArgs e)
        {
            if (mFirmata != null && !mReportLocal)
                mFirmata.sendSetPinReporting(mPin, checkBoxReporting.Checked);
            mReportLocal = false;
        }

    }
}
